<?php
// echo 123;exit;
use App\Models\CommonModel;
use App\Models\UserModel;
use Illuminate\Support\Facades\DB;
//簡單驗證
//判斷是否md5加密
function is_md5($password) {
    return preg_match("/^[a-z0-9]{32}$/", $password);
}
//判斷是否手機號
function is_phone($phone) {
    return preg_match("/^[1][3578][0-9]{9}$/", $phone);
}
//判斷是否郵箱
function is_email($email) {
    return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email);
}
// 判斷是否為空
function ifempty($tar, $def = 0){
    if(empty($tar)){
        return $def;
    }else{
        return $tar;
    }
}

function userMoneyLog($uid,$type,$amoney,$order_id="",$info=""){
    $xva = floatval($amoney);
    if (empty($xva)) return true;
    $done = false;
    $MM = UserModel::getMoney($uid);
    if(empty($MM)) DB::table('user_money')->insert(['uid'=>$uid]);
    if ($order_id>0){
        $order_id = $order_id;
    }else{
        $order_id = 0;
    }
    $data['uid'] = $uid;
    $data['type'] = $type;
    $data['info'] = $info;
    $data['add_time'] = time();
    $data['add_ip'] = $_SERVER['REMOTE_ADDR'];
    $data['order_id'] = $order_id;
    switch ($type) {
        case 1://支付保證金
            $data['affect_money'] = -$amoney;
            $data['account_money'] = ifempty($MM->account_money) - ($amoney);
            $data['collect_money'] = ifempty($MM->collect_money) - ($amoney);
            $data['freeze_money'] = ifempty($MM->freeze_money);

            $mmoney['account_money'] = ifempty($MM->account_money) - ($amoney);
            $mmoney['collect_money'] = ifempty($MM->collect_money) - ($amoney);
            break;
        case 2://充值
            $data['affect_money'] = $amoney;
            $data['account_money'] = ifempty($MM->account_money) + ($amoney);
            $data['collect_money'] = ifempty($MM->collect_money) + ($amoney);
            $data['freeze_money'] = ifempty($MM->freeze_money);

            $mmoney['account_money'] = ifempty($MM->account_money) + ($amoney);
            $mmoney['collect_money'] = ifempty($MM->collect_money) + ($amoney);
            break;
        case 3://轉出提現-成功
            $data['affect_money'] = -$amoney;
            $data['freeze_money'] = ifempty($MM->freeze_money) - ($amoney);
            $data['account_money'] = ifempty($MM->account_money);
            $data['collect_money'] = ifempty($MM->collect_money);

            $mmoney['freeze_money'] = ifempty($MM->freeze_money) - ($amoney);
            break;
        case 4://申請提現
            $data['affect_money'] = -($amoney);
            $data['account_money'] = ifempty($MM->account_money) - ($amoney);
            $data['freeze_money'] = ifempty($MM->freeze_money) + ($amoney);
            $data['collect_money'] = ifempty($MM->collect_money);

            $mmoney['account_money'] = ifempty($MM->account_money) - ($amoney);
            $mmoney['freeze_money'] = ifempty($MM->freeze_money) + ($amoney);
            break;
        case 5://轉出提現-失敗
            $data['affect_money'] = ($amoney);
            $data['account_money'] = ifempty($MM->account_money) + ($amoney);
            if($MM->freeze_money>0){
                $data['freeze_money'] = ifempty($MM->freeze_money) - ($amoney);
            }
            $data['collect_money'] = ifempty($MM->collect_money);
            $mmoney['account_money'] = ifempty($MM->account_money) + ($amoney);
            if($MM->freeze_money>0){
                $mmoney['freeze_money'] = ifempty($MM->freeze_money) - ($amoney);
            }
            break;
        default:
            $data['affect_money'] = $amoney;
            $data['account_money'] = ifempty($MM->account_money) + $amoney;
            $data['collect_money'] = ifempty($MM->collect_money);
            $data['freeze_money'] = ifempty($MM->freeze_money);

            $mmoney['account_money'] = ifempty($MM->account_money) - ($amoney);
            $mmoney['collect_money'] = ifempty($MM->collect_money) - ($amoney);
            break;
    }
    $newid = DB::table('user_moneylog')->insertGetId($data);

    //帳戶更新

    if ($newid) $xid = DB::table('user_money')->where('uid','=',$uid)->update($mmoney);

    if ($xid) {
        $done = true;
    } else {
        $done = false;
    }

    return $done;

}
/**
* +----------------------------------------------------------
* 產生隨機字串，可用來自動生成密碼 默認長度6位 字母和數字混合
* +----------------------------------------------------------
* @param string $len 長度
* @param string $type 字串類型
* 0 字母 1 數字 其它 混合
* @param string $addChars 額外字符
* +----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function rand_string($ukey = "", $len = 6, $type = '1', $utype = '1', $addChars = ''){
	$str = '';
	switch ($type) {
	    case 0:
	        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
	        break;
	    case 1:
	        $chars = str_repeat('0123456789', 3);
	        break;
	    case 2:
	        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
	        break;
	    case 3:
	        $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
	        break;
	    default :
	        // 默認去掉了容易混淆的字符oOLl和數字01，要添加請使用addChars參數
	        $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
	        break;
	}
	if ($len > 10) {//位數過長重復字符串壹定次數
	    $chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);
	}
	$chars = str_shuffle($chars);
	$str = substr($chars, 0, $len);
	if (!empty($ukey)) {
	    $vd['code'] = $str;
	    $vd['send_time'] = time();
	    $vd['ukey'] = $ukey;
	    $vd['type'] = $utype;
	    DB::table('verify')->insert($vd);
	}
	return $str;
}
/**
* +----------------------------------------------------------
* 驗證是否通過
* +----------------------------------------------------------

* $ukey：唯壹標識，UID或者郵箱或者手機
* $code：驗證碼 
* $utype：1表示激活鏈接驗證 
* $timespanL:驗證碼有效時間 
**/
function is_verify($ukey, $code, $utype, $timespan){
	if (!empty($ukey)) $vd['ukey'] = $ukey;
	$vd['type'] = $utype;
	// DB::connection()->enableQueryLog();
	$vo = DB::table("verify")->where($vd)->where('send_time','>',time()-$timespan)->select('ukey','code')->orderBy('id','desc')->first();

	// $queries = DB::getQueryLog();
	// dd($queries);
	// dd($vo);
	if (!empty($vo) && $vo->code == $code) return $vo->ukey;
	else return false;
}
// 用護登錄日誌
function userLog($uid, $ip, $operation, $bz, $type){
    $ip = $_SERVER['REMOTE_ADDR'];
    if ($ip == "app") {
        $ip = "app";
    }
    if (isset($uid) && $uid != null) {
        $data = [
            'uid' => $uid,
            'create_time' => time(),
            'ip' => $ip,
            'operation' => $operation,
            'bz' => $bz,
            'type' => $type,
        ];
        $aa = CommonModel::addData('user_log',$data);
    }
    return $aa;
}
// 记录日志
function saveCommonLog($recordId, $logContent, $tableName, $userName, $userId, $opType, $logType="SYSTEM", $userType="ADMIN"){
    $now = date("Y-m-d H:i:s", time());
    $logData["record_id"] = $recordId;
    $logData["log_content"] = "[".$userName."]在".$now.$logContent;
    $logData["log_table"] = $tableName;
    $logData["log_type"] = $logType;
    $logData["op_type"] = $opType;
    $logData["user_type"] = $userType;
    $logData["create_time"] = $now;
    $logData["create_id"] = $userId;
    CommonModel::addData('operation_log',$logData);
}

?>